package org.example.carplatoon.utils;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.example.carplatoon.common.CustomException;

/*
    利用qq邮箱发送短信
 */
@Slf4j
public class SMSUtils {

    public static void sendEmail(String email, String userName, String name, String code) {

        HtmlEmail send = new HtmlEmail();

        try {
            send.setHostName("smtp.qq.com");
//            send.setAuthentication("2265904238@qq.com","lerysmilaygqdibf");
            send.setAuthentication("3296932897@qq.com","jtqonfruqcjkdafc");
            send.setFrom("3296932897@qq.com","carplatoon");
            send.setSSLOnConnect(true);
            send.setCharset("utf-8");
            send.addTo(email);
            send.setSubject("【车辆编队管理系统】验证码");
            send.setMsg(String.format("【车辆编队管理系统】%s (%s) 您好，您的验证码为：<font color='red' >" + code + " </font>，验证码用户用户注册，" +
                    "<font color='red' >5分钟</font>内有效，请勿泄露和转发，如非本人操作，请忽略此邮件。", userName,name));
            send.send();
            log.info("验证码发送成功");
        } catch (EmailException e) {
            throw new CustomException("邮箱验证码发送错误");
        }
    }
}
